package tasks;

import java.util.Properties;
import java.util.regex.*;

import lombok.*;
import com.googlecode.flyway.core.Flyway;
import com.googlecode.flyway.core.api.FlywayException;
import org.postgresql.jdbc2.optional.SimpleDataSource;
import com.googlecode.flyway.core.util.ExceptionUtils;
import com.googlecode.flyway.core.util.jdbc.DriverDataSource;

import ilender.util.*;

public final class Migrate{
	/*Instantiation Guard*/private Migrate(){}

	/**
	 * Perform the database migration.
	 *
	 * @param args First argument must be the path for the migrations directory.
	 */
	public static void main(String[] args) throws Exception{
		Properties dbProperties=PropertiesTools.fetchDatabaseProperties();

		Flyway flyway=new Flyway();
		flyway.setDataSource(new DriverDataSource(
					dbProperties.getProperty("database.driver"),
					dbProperties.getProperty("database.url"),
					dbProperties.getProperty("database.user"),
					dbProperties.getProperty("database.password")));
		flyway.setLocations("filesystem:"+args[0]);
		try{
			flyway.migrate();
		}catch(Exception e){
			System.out.println("FAILED!!!");
			System.out.println(e.getMessage());
			System.out.println(ExceptionUtils.getRootCause(e));
		}
	}
}
